#include "stdafx.h"

#ifdef _V_INC_SVN_VER_
#include "version.h"
#endif

#ifdef _V_INC_BUILD_
#include "build.h"
#endif

#include <QxtCore/QxtLogger>


#include "vdummyview.h"
#include "vblankeditorview.h"

#include "../VSettings/vsettings.h"
#include "../VCommon/vviewmanager.h"
#include "../VSql/VSqlDatabase.h"
#include "../VSql/VNav.h"

#include "vmainwindow.h"

namespace
{
	const QString VIEW_DUMMY1 = "dummyView1";
	const QString VIEW_DUMMY2 = QObject::tr("Test VSqlSelect");
	const QString VIEW_SQL_NAVIGATOR = QObject::tr("SQL Navigator");
	const QString VIEW_BLANKEDITOR = QObject::tr("Blank Editor");
	const QString VIEW_NAVIGATOR = QObject::tr("Navigator");
}

VMainWindow::VMainWindow(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	
	ui.setupUi(this);
	
	QString title(QString("Rev %1").arg(VIDAR_SVN_VERSION));

#ifdef _V_INC_BUILD_
	title.append(QString(" , build %1").arg(VIDAR_BUILD_NUMBER));
#endif

	setWindowTitle(title);

	m_viewMgr = new VViewManager(ui.menu_Mode, this);
	
	VSettings settings(QObject::metaObject()->className());
	restoreGeometry(settings.value("Geometry").toByteArray());

	//qxtLog->info("Debug message");
}

VMainWindow::~VMainWindow()
{
	VSettings settings(QObject::metaObject()->className());
	settings.setValue("Geometry", saveGeometry());
	settings.setValue("ActiveViewName", m_viewMgr->activeView()->name());
}

void VMainWindow::closeEvent(QCloseEvent *event)
{
	/*if (maybeSave()) {
		writeSettings();
		event->accept();
	} else {
		event->ignore();
	}*/

	event->accept();
}

void VMainWindow::hover(QAction* a)
{
	setWindowTitle(a->text());
}

void VMainWindow::trigger(QAction* a)
{
	setWindowTitle(a->text());
}

void VMainWindow::setupQss()
{
	QString styleSheet =
	"QToolBar {"
		"background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(241,248,254,10%), stop: 1 rgb(241,248,254,70%));"
		//"border-radius: 10px;"
		//"border: 100px transparent;"
	"}"
	"QToolButton {"
		"font-size:13px;"
		"color:blue;"
	"}"
	"QToolButton:disabled {"
		"color:gray;"
	"}"

	"QHeaderView {"
		"font-size:13px;"
		//"height:13px;"
	"}"

	"QTreeView {"
		//"font-family:arial;"
		"font-size:13px;"
		//"background: rgb(255,255,255,20%);"
		//"background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(255,255,255,100%), stop: 1 rgb(0,0,0,10%));"
	"}"
	"QTableView {"
		//"font-family:arial;"
		"font-size:13px;"
		//"background: rgb(255,255,255,20%);"
		//"background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(255,255,255,100%), stop: 1 rgb(0,0,0,10%));"
	"}"
	;
	
	/*

	"QTextEdit {"
	"font-size:18px;"
	"}"*/

	setStyleSheet(styleSheet);
}

void VMainWindow::setupEnv()
{
	if (VSqlDatabase::main().isOpen())
	{
		m_viewMgr->registerView(new VNavView(VIEW_NAVIGATOR));
	}
	
	m_viewMgr->registerView(new VBlankEditorView(VIEW_BLANKEDITOR));
	
	VSettings settings(metaObject()->className());
	QString activeViewName = settings.value("ActiveViewName", VIEW_NAVIGATOR).toString();
	m_viewMgr->setActiveView(activeViewName);
}